#include <func.h>
#include<string.h>

int print_dir(char* path,int width)//width作用是控制输出格式加空格
{
    DIR* dir;
    dir=opendir(path);
    ERROR_CHECK(dir,NULL,"opendir")
    struct dirent *p;
    char buf[512];
    while((p=readdir(dir)))
    {
        if(!strcmp(".",p->d_name)||!strcmp("..",p->d_name));//如果是.或者..则什么都不做，防止程序死循环
        else
        {
            printf("%*s%s\n",width,"",p->d_name);
            if(4==p->d_type)
            {
                memset(buf,0,sizeof(buf));
                sprintf(buf,"%s%s%s",path,"/",p->d_name);//复制相对于父程序的相对路径
                print_dir(buf,width+4);
            }
        }
        
    }
    closedir(dir);
    return 0;
}

int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,2)
    print_dir(argv[1],0);
    return 0;
}